package login;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;

@WebServlet("/index")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(false);  // 如果没有则返回null
        if (session == null) {
            resp.setContentType("text/html; charset=utf-8");
            resp.getWriter().write("您当前尚未登录成功!");
            return;
        }
        // 从会话中获取之前存储的用户信息
        String username = (String) session.getAttribute("username");
        String loginTime = (String) session.getAttribute("loginTime"); // 直接获取格式化的时间字符串

        // 生成一个页面 将数据显示到页面上
        resp.setContentType("text/html; charset=utf-8");
        String respBody = "欢迎您 " + username + "，本次登录时间为: " + loginTime;
        resp.getWriter().write(respBody);
    }
}
